package com.imooc.joe_springmvc1.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 编写切面:用来做日志记录的
 * Created by joe on 16-8-9.
 */
@Aspect//声明是一个切面
@Component//通过这个注解让此切面成为Spring容器管理的Bean
public class LogAspect {
    @Pointcut("@annotation(com.imooc.joe_springmvc1.aop.Action)")//声明切点
    public void annotationPointCut(){}

    @After("annotationPointCut()")//声明一个建言,并使用上面声明的切点
    public void after(JoinPoint joinPoint){
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Action action = method.getAnnotation(Action.class);//通过反射获得注解上的属性,然后做日志记录相关的操作
        System.out.println("注解式拦截 "+action.name());
    }





//    @Before("execution( * com.imooc.joe_springmvc1.aop.DemoMethodService.*(..))")
//    public void before(JoinPoint joinPoint){
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        System.out.println("方法规则式拦截 "+method.getName());
//    }
}
